//
//  LSCupIntergralTableHeaderView.m
//  LookScore
//
//  Created by nice on 2018/7/5.
//  Copyright © 2018年 ChongQingXunTiCompany. All rights reserved.
//

#import "LSCupIntergralTableHeaderView.h"
#import "LSCupIntegralModel.h"
#define Start_X          LSAutoVlaue(15.0f,LSScreenHeight)      // 第一个按钮的X坐标
#define Start_Y          LSAutoVlaue(46.0f,LSScreenHeight)     // 第一个按钮的Y坐标
#define Width_Space      LSAutoVlaue(30.0f,LSScreenWidth)      // 2个按钮之间的横间距
#define Height_Space     LSAutoVlaue(20.0f,LSScreenHeight)     // 竖间距
#define Button_Width     (LSScreenWidth-2*Start_X-3*Width_Space)/4    // 宽
#define Button_Height    LSAutoVlaue(30.0f,LSScreenHeight)     // 高
@implementation LSCupIntergralTableHeaderView{
    UIButton * checkMoreBtn;
    NSInteger selectTag;
    BOOL isOpen;
    UIButton *selectedBtn;
    UILabel * _nowLable;
    UIView * _headerView;
    NSMutableArray * _btnArray;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        [self addContentView];
    }
    
    return self;
}


- (void)addContentView{
    
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = LSSubGrayColorf7;
    _headerView = headerView;
    [self addSubview:headerView];
    headerView.sd_layout
    .leftEqualToView(self)
    .rightEqualToView(self)
    .topEqualToView(self);
    
    UILabel *stageLable = [[UILabel alloc] init];
    stageLable.text = @"杯赛阶段";
    stageLable.font = LSFontSize12;
    stageLable.textAlignment = NSTextAlignmentLeft;
    stageLable.textColor = RGB(51, 51, 51);
    [headerView addSubview:stageLable];
    stageLable.sd_layout
    .leftEqualToView(headerView).offset(LSAutoVlaue(15, LSScreenWidth))
    .topEqualToView(headerView).offset(LSAutoVlaue(11, LSScreenHeight))
    .widthIs(LSAutoVlaue(80, LSScreenWidth))
    .heightIs(LSAutoVlaue(12, LSScreenHeight));
    
    UILabel *nowLable = [[UILabel alloc] init];
    
    nowLable.font = LSFontSize12;
    nowLable.textAlignment = NSTextAlignmentRight;
    nowLable.textColor = RGB(51, 51, 51);
    [headerView addSubview:nowLable];
    _nowLable = nowLable;
    nowLable.sd_layout
    .rightSpaceToView(headerView, 5)
    .centerYEqualToView(stageLable)
    .heightIs(LSAutoVlaue(12, LSScreenHeight));
    [nowLable setSingleLineAutoResizeWithMaxWidth:180];
    
    checkMoreBtn = [[UIButton alloc] init];
    [checkMoreBtn setTitle:@"展开更多" forState:UIControlStateNormal];
    checkMoreBtn.titleLabel.font = LSFontSize(12, LSScreenWidth);
    [checkMoreBtn setTitleColor:RGB(102, 102, 102) forState:UIControlStateNormal];
    [checkMoreBtn setImage:[UIImage imageNamed:@"PullDown"] forState:UIControlStateNormal];
    [checkMoreBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -20, 0, 20)];
    [checkMoreBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 40, 0, -40)];
    [checkMoreBtn addTarget:self action:@selector(clickCheckMoreButton:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:checkMoreBtn];
    checkMoreBtn.sd_layout
    .centerXEqualToView(headerView)
    .bottomSpaceToView(headerView, 5)
    .widthIs(LSAutoVlaue(80, LSScreenWidth))
    .heightIs(LSAutoVlaue(30, LSScreenHeight));
    
    _btnArray = @[].mutableCopy;
    
    if(!isOpen){
//        NSMutableArray *Arr = @[].mutableCopy;
////        for (NSDictionary *dic in self.kindListArray) {
////            [Arr addObject:dic[@"KindName"]];
////        }
//        NSArray *titleArr;
//        if (Arr.count != 0) {
//            titleArr = [Arr subarrayWithRange:NSMakeRange(0, 4)];
//        }else{
//            titleArr = @[];
//        }
//
//        //        NSArray *titleArr = @[@"第一圈",@"第二圈",@"第三圈",@"附加赛"];
//        headerView.frame = CGRectMake(0, 0, LSScreenWidth, 116);
//        for (int i = 0; i < titleArr.count; i ++) {
//            NSInteger index = i % 4;
//            NSInteger page = i / 4;
//
//            UIButton *stageBtn = [[UIButton alloc] initWithFrame:CGRectMake(index * (Button_Width + Width_Space) + Start_X, page  * (Button_Height + Height_Space)+Start_Y, Button_Width, Button_Height)];
//            stageBtn.tag = i+100;
//            //默认选中第一个
//            if (stageBtn.tag==selectTag) {
//                [stageBtn setBackgroundImage:[UIImage imageNamed:@"StageButtonSelected"] forState:UIControlStateNormal];
//                [stageBtn setTitleColor:LSTitleWhiteColor forState:UIControlStateNormal];
//                selectedBtn=stageBtn;
//            }else{
//                [stageBtn setBackgroundImage:[UIImage imageNamed:@"StageButton"] forState:UIControlStateNormal];
//                [stageBtn setTitleColor:LSSubColor5D5D5D forState:UIControlStateNormal];
//            }
//            [stageBtn setTitle:titleArr[i] forState:UIControlStateNormal];
//
//            stageBtn.titleLabel.font = LSFontSize12;
//            [stageBtn  addTarget:self action:@selector(clickStageButton:) forControlEvents:UIControlEventTouchUpInside];
//            [headerView addSubview:stageBtn];
//            
//
//            checkMoreBtn.sd_layout
//            .centerXEqualToView(headerView)
//            .bottomEqualToView(headerView).offset(-LSAutoVlaue(10, LSScreenHeight))
//            .widthIs(LSAutoVlaue(80, LSScreenWidth))
//            .heightIs(LSAutoVlaue(30, LSScreenHeight));
//        }
//    }else{
//        //        NSArray *titleArr = @[@"第一圈",@"第二圈",@"第三圈",@"附加赛",@"分组赛",@"十六强",@"半准决赛",@"准决赛",@"决赛"];
//        headerView.frame = CGRectMake(0, 0, LSScreenWidth, 226);
//
    }
    
}

- (void)setKindListArray:(NSMutableArray *)kindListArray{
    _kindListArray = kindListArray;
    for (int i = 0; i < kindListArray.count; i ++) {
        KindListModel * model = self.kindListArray[i];
        NSInteger index = i % 4;
        NSInteger page = i / 4;
        
        UIButton *stageBtn = [[UIButton alloc] initWithFrame:CGRectMake(index * (Button_Width + Width_Space) + Start_X, page  * (Button_Height + Height_Space)+Start_Y, Button_Width, Button_Height)];
        [_btnArray addObject:stageBtn];
        if (i>3) {
            stageBtn.hidden = YES;
        }
        stageBtn.tag = i+100;
//        if () {
//            selectTag = i;
//        }
        
        //默认选中第一个
        if (model.IsCurrent) {
            selectTag = i;
            [stageBtn setBackgroundImage:[UIImage imageNamed:@"StageButtonSelected"] forState:UIControlStateNormal];
            [stageBtn setTitleColor:LSTitleWhiteColor forState:UIControlStateNormal];
            _nowLable.text = [NSString stringWithFormat:@"目前进行 %@",model.KindName];
            selectedBtn=stageBtn;
        }else{
            [stageBtn setBackgroundImage:[UIImage imageNamed:@"StageButton"] forState:UIControlStateNormal];
            [stageBtn setTitleColor:LSSubColor5D5D5D forState:UIControlStateNormal];
        }
        
        [stageBtn setTitle:model.KindName forState:UIControlStateNormal];
        stageBtn.titleLabel.font = LSFontSize12;
        [stageBtn  addTarget:self action:@selector(clickStageButton:) forControlEvents:UIControlEventTouchUpInside];
        [_headerView addSubview:stageBtn];
        
//        [checkMoreBtn setImage:[UIImage imageNamed:@"PullUp"] forState:UIControlStateNormal];
//        checkMoreBtn.sd_layout
//        .centerXEqualToView(_headerView)
//        .bottomEqualToView(_headerView).offset(-LSAutoVlaue(40, LSScreenHeight))
//        .widthIs(LSAutoVlaue(100, LSScreenWidth))
//        .heightIs(LSAutoVlaue(30, LSScreenHeight));
        
        
    }
    
    NSInteger page = kindListArray.count / 4 + 1;
    CGFloat headderH = page  * (Button_Height + Height_Space);
    if (kindListArray.count > 4) {
        _headerView.sd_layout.heightIs(headderH/2 +Start_Y + LSAutoVlaue(30, LSScreenHeight));
        self.width = LSScreenWidth;
        self.height = headderH/2+Start_Y;
        self.fixedHeight = @(headderH/2+Start_Y + LSAutoVlaue(30, LSScreenHeight));
        self.fixedWidth = @(LSScreenWidth);
    }else{
        
        _headerView.sd_layout.heightIs(headderH+Start_Y + LSAutoVlaue(30, LSScreenHeight));
        self.width = LSScreenWidth;
        self.height = headderH+Start_Y;
        self.fixedHeight = @(headderH+Start_Y + LSAutoVlaue(30, LSScreenHeight));
        self.fixedWidth = @(LSScreenWidth);
    }
    
}

- (void)clickStageButton:(UIButton *)sender{
    if (selectedBtn) {
        [selectedBtn setBackgroundImage:[UIImage imageNamed:@"StageButton"] forState:UIControlStateNormal];
        [selectedBtn setTitleColor:LSSubColor5D5D5D forState:UIControlStateNormal];
    }
    selectedBtn = sender;
    [selectedBtn setBackgroundImage:[UIImage imageNamed:@"StageButtonSelected"] forState:UIControlStateNormal];
    [selectedBtn setTitleColor:LSTitleWhiteColor forState:UIControlStateNormal];
    if (self.btnClickBlock) {
        self.btnClickBlock(sender.tag -100);
    }
}

- (void)clickCheckMoreButton:(UIButton *)sender{
     NSInteger page = self.kindListArray.count / 4 + 1;
     CGFloat headderH = page  * (Button_Height + Height_Space);
    isOpen = !isOpen;
    if (isOpen) {
        if (self.kindListArray.count > 4) {
            _headerView.sd_layout.heightIs(headderH+Start_Y + LSAutoVlaue(30, LSScreenHeight));
            self.width = LSScreenWidth;
            self.height = headderH+Start_Y;
            self.fixedHeight = @(headderH+Start_Y + LSAutoVlaue(30, LSScreenHeight));
            self.fixedWidth = @(LSScreenWidth);
        }
        [checkMoreBtn setImage:[UIImage imageNamed:@"PullUp"] forState:UIControlStateNormal];
    }else{
        [checkMoreBtn setImage:[UIImage imageNamed:@"PullDown"] forState:UIControlStateNormal];
        _headerView.sd_layout.heightIs(headderH/2+Start_Y + LSAutoVlaue(30, LSScreenHeight));
        self.width = LSScreenWidth;
        self.height = headderH/2+Start_Y;
        self.fixedHeight = @(headderH/2+Start_Y + LSAutoVlaue(30, LSScreenHeight));
        self.fixedWidth = @(LSScreenWidth);
    }
    [self btnIsHidden:_btnArray with:isOpen];
    if (self.moreBtnBlock) {
        self.moreBtnBlock();
    }
    
}
- (void)btnIsHidden:(NSMutableArray *)array with:(BOOL)isOpen{
    if (isOpen) {
        [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
           UIButton * btn = obj;
            btn.hidden = NO;
        }];
    }else{
        [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            UIButton * btn = obj;
            
            if (idx>3) {
                btn.hidden = YES;
            }
        }];
    }
}

@end
